<?php

require_once(ROOT . DS . 'config' . DS . 'Configuracao.php');
require_once(ROOT . DS . 'lib' . DS . 'Excecao.php');

class Conexao extends PDO {
 
    public static $instancia;
	
	private static $dsn;

	private static $config;


	public function __construct() {
		$this->config = new Configuracao;
		
		$this->dsn = $this->config->db_driver . ':dbname=' . $this->config->db_name . ';host=' . $this->config->db_host;

		try {
			$this->instancia = new PDO($this->dsn, $this->config->db_usuario, $this->config->db_senha);
		} catch (Exception $e) {
			throw new Excecao($e->getMessage(), $e->getCode(), $e->getFile(), $e->getLine());
			exit;
		}
	}
 
    public function getInstance() {
        if(!isset($this->instancia)) {
            try {
                $this->__construct();
            } catch (Exception $e) {
                echo 'Erro ao conectar';
                exit();
            }
        }
		return $this->instancia;
    }
	
	function query($query) {
		return $this->instancia->query($query);
	}
	
}
?>